import random from otree.api import * """ Sim for Hawk Dove Game except with random determined Possessor Challenger roles """ class Constants(BaseConstants): name_in_url = 'HawkDovePossessor' players_per_group = 2 num_rounds = 10 instructions_template = 'ultimatum/instructions.html' # role = random.choice([1, 2]) # endowment m = 25 # value of resource v = 50 # cost of conflict (each) c = 50 # cost of delay (each) a = 10 # payoff if 1 player fights and the other retreats""", Hawk_payoff = 75 # Hawk_payoff = Constants.m + Constants.v Dove_payoff = 25 # Dove_payoff = Constants.m # payoff if both players fight or both retreat Fight_payoff = 0 # Fight_payoff = Constants.m + Constants.v/2 - Constants.c Split_payoff = 40 # Split_payoff = Constants.m + Constants.v/2 - Constants.a class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): decision = models.StringField( choices=['Fight', 'Do NOT Fight'], doc="""This player's decision""", widget=widgets.RadioSelect, ) # FUNCTIONS def creating_session(subsession: Subsession): subsession.group_randomly() def role(group: Group): return random.choice([1, 2]) # role = random.choice([1, 2]) def role(player: Player): return 'Possessor' if player.id_in_group == 1 else 'Challenger' # return 'Possessor' if self.id_in_group == self.group.role else 'Challenger' # return 'Possessor' if self.id_in_group == Constants.role else 'Challenger' def other_player(player: Player): return player.get_others_in_group()[0] def set_payoff(player: Player): payoff_matrix = { 'Fight': { 'Fight': Constants.m + Constants.v / 2 - Constants.c, 'Do NOT Fight': Constants.m + Constants.v, }, 'Do NOT Fight': { 'Fight': Constants.m, 'Do NOT Fight': Constants.m + Constants.v / 2 - Constants.a, }, } player.payoff = payoff_matrix[player.decision][other_player(player).decision] # PAGES class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Decision(Page): form_model = 'player' form_fields = ['decision'] class ResultsWaitPage(WaitPage): title_text = ' ' body_text = 'Please wait for the other player to make a decision.' @staticmethod def after_all_players_arrive(group: Group): for p in group.get_players(): p.set_payoff() class Results(Page): @staticmethod def vars_for_template(player: Player): me = player opponent = me.other_player() return { 'my_decision': me.decision, 'opponent_decision': opponent.decision, 'opponent_payoff': opponent.payoff, 'same_choice': me.decision == opponent.decision, } page_sequence = [Introduction, Decision, ResultsWaitPage, Results]